////////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2020 Alexander Stukowski
//
//  This file is part of OVITO (Open Visualization Tool).
//
//  OVITO is free software; you can redistribute it and/or modify it either under the
//  terms of the GNU General Public License version 3 as published by the Free Software
//  Foundation (the "GPL") or, at your option, under the terms of the MIT License.
//  If you do not alter this notice, a recipient may use your version of this
//  file under either the GPL or the MIT License.
//
//  You should have received a copy of the GPL along with this program in a
//  file LICENSE.GPL.txt.  You should have received a copy of the MIT License along
//  with this program in a file LICENSE.MIT.txt
//
//  This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
//  either express or implied. See the GPL or the MIT License for the specific language
//  governing rights and limitations.
//
////////////////////////////////////////////////////////////////////////////////////////

#pragma once


#include <ovito/gui/desktop/GUI.h>
#include <ovito/core/oo/RefTargetListener.h>

namespace Ovito {

/**
 * A Qt model/view system list model that contains all scene nodes in the current scene.
 */
class OVITO_GUI_EXPORT SceneNodesListModel : public QAbstractListModel
{
	Q_OBJECT

public:

	/// Constructs the model.
	SceneNodesListModel(DataSetContainer& datasetContainer, QWidget* parent = 0);

	/// Returns the number of rows of the model.
	virtual int rowCount(const QModelIndex & parent = QModelIndex()) const override;

	/// Returns the model's data stored under the given role for the item referred to by the index.
	virtual QVariant data(const QModelIndex & index, int role) const override;

	/// Returns the item flags for the given index.
	virtual Qt::ItemFlags flags(const QModelIndex& index) const override;

private Q_SLOTS:

	/// This is called when a new dataset has been loaded.
	void onDataSetChanged(DataSet* newDataSet);

	/// This handles reference events generated by the root node.
	void onRootNodeNotificationEvent(const ReferenceEvent& event);

	/// This handles reference events generated by the scene nodes.
	void onNodeNotificationEvent(RefTarget* source, const ReferenceEvent& event);

private:

	/// The container of the dataset.
	DataSetContainer& _datasetContainer;

	/// This helper object is used to listen for reference events generated by scene nodes.
	VectorRefTargetListener<SceneNode> _nodeListener;

	/// This helper object is used to listen for reference events generated by the root node.
	RefTargetListener<RootSceneNode> _rootNodeListener;
};

}	// End of namespace
